package vg.modules.zoom.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;

import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.user_interface_manager.additional_swing_components.ZoomComboBox;

/**
 * This class realizes module for zoom.
 * @author tzolotuhin
 */
public class ZoomBox {
	// Main components
	private final ZoomComboBox element;
	// Data
	private AGraphView view;
	// Mutex
	private final Object theMutexObject;
	/**
	 * Constructor.
	 */
	public ZoomBox() {
		// init mutex
		this.theMutexObject = new Object();
		// init components
		this.element = new ZoomComboBox();
		this.element.setToolTipText("Scaling of graph's view");
		this.view = null;
		this.element.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (ZoomBox.this.theMutexObject) {
					//if (ZoomBox.this.view != null)
					//	ZoomBox.this.view.setZoom(ZoomBox.this.element.getScale());
				}
			}
		});			
	}
	public JComponent getView() {
		return(this.element.getView());
	}
	/**
	 * This method changes current view.
	 */
	public void changeView(final AGraphView newView) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				synchronized (ZoomBox.this.theMutexObject) {
					ZoomBox.this.view = newView;
					if (ZoomBox.this.view != null) {
						//ZoomBox.this.view.setEnableZoomComboToGraphView(true, ZoomBox.this.element);
						ZoomBox.this.element.enable();
					} else {
						ZoomBox.this.element.disable();
					}
				}
			}
		});
	}
}
